package com.lxr.activity;


	import java.util.ArrayList;
	import java.util.List;  
    import java.util.Map;

    import android.content.Intent;
	import android.content.pm.ActivityInfo;  
	import android.os.Bundle;  
	import android.util.Log;  
	import android.view.View;  
	import android.view.Window;  
	import android.view.View.OnClickListener;  
	import android.widget.ArrayAdapter;
    import android.widget.Button;  
	import android.widget.ListView;
    import android.widget.Toast;
	import android.app.ListActivity; 
	import com.wxq.draw.DrawConstants;
	import com.wxq.draw.DrawDBTool;
	import com.wxq.schoolmap.R;


	public class GetPicActivity extends ListActivity {
	    private List<String> items = null;//存放名称  
	    private List<String> paths = null;//存放路径   
		private final static String TAG = "CameraActivity";  
	    private Button btnSave,btnback;  
	    private String ID;
	    private DrawDBTool dbTool;
	    private String imageinfo;
	    
	      
	    @Override  
	    public void onCreate(Bundle savedInstanceState) {  
	        super.onCreate(savedInstanceState);  
	        Log.i(TAG,"activity created!!!!!!!");
	        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
	        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
	        setContentView(R.layout.get_pic);  
	        
	        
	        Intent intent=getIntent();
	        Bundle bundle=intent.getExtras();
	        ID=bundle.getString("spotId");
	       
	        Log.i(TAG,ID+"lllllllllllllll");
	       
	       
	        setupViews();  
	    }  
	      
	    protected void onResume() {
	    	super.onResume();
	    	Log.d("Infor", "我靠！！！我resume了...");
	    	showlist();
	    }

	    
	    private void setupViews(){  
	        dbTool = new DrawDBTool(this);
	        dbTool.open();
	        
	        btnSave = (Button) findViewById(R.id.tocamera);
	        btnback = (Button) findViewById(R.id.back1);
	        
	        btnSave.setOnClickListener(new OnClickListener() {  
	            public void onClick(View v) {  
	            	Intent intent = new Intent();
	    			intent.putExtra("spotId2", ID);
	    			intent.setClass(GetPicActivity.this, CameraActivity.class);
	    			GetPicActivity.this.startActivity(intent);
	                }  
	        });  
	        btnback.setOnClickListener(new OnClickListener() {  
	            public void onClick(View v) { 
	            	
	            	finish();    
	            }  
	        });    

	  }
	       
	    public void showlist(){
	      Map<String, String> spotInfo = dbTool.getSpotInfo(ID);
	      imageinfo=spotInfo.get("image");
	      String imagename="";
	      try{    
	    	  if(!imageinfo.isEmpty()){
		        items = new ArrayList<String>();   
		        paths = new ArrayList<String>();
		       for(int i=0;i<imageinfo.length();i++){ 
		    	   for(;imageinfo.charAt(i)!=';';i++){
		    		   imagename+=imageinfo.charAt(i);
		    	   }
		    	   items.add(imagename);
			       paths.add(android.os.Environment.getExternalStorageDirectory()+DrawConstants.ImagesFolder+"/"+imagename);    ///////////////////////////////
		         imagename="";
		       }
		        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);   ///////////////////////////////
		        this.setListAdapter(adapter); 
	    	  }else{nopicture();}
		    }  
		    catch(Exception ex){nopicture();} 		    
	      imageinfo="";
	    }
	    protected void nopicture(){
	    	Toast toast = Toast.makeText(this, "该地点还没有照片!\n请点击拍照按钮添加", Toast.LENGTH_SHORT); 
	    	  toast.setGravity(0|0, 80, 80); 
	    	  toast.setMargin(-0.5f, 0f); 
	    	  toast.show();
	    }
	    
	    @Override    
	    protected void onListItemClick(ListView l, View v, int position, long id)   ///////////////
	    {    
	        super.onListItemClick(l, v, position, id);    
	        String path = paths.get(position);    
	        
			Intent intent = new Intent();
			intent.putExtra("path", path);
			intent.putExtra("position", position);
			intent.putExtra("ID", ID);
			intent.setClass(GetPicActivity.this, PictureActivity.class);
			GetPicActivity.this.startActivity(intent);
	    }    
	    
	    @Override
		protected void onDestroy() {
			dbTool.close();
			super.onDestroy();
		}	
	}  